package newhiant.hianto2o.com.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import newhiant.hianto2o.com.model.Member;

/**
 * Created by Administrator on 2017/8/2 0002.
 */

public class RVAdapter2 extends RecyclerView.Adapter<RVAdapter2.RvHoldView> {

    private int mLayoutId;      // 条目布局文件ID
    private int mVariableId;    // DataBinding变量ID
    private Context mContext;
    private List<Member> members;

    public RVAdapter2(int mLayoutId, int mVariableId, Context mContext, List<Member> members) {
        this.mLayoutId = mLayoutId;
        this.mVariableId = mVariableId;
        this.mContext = mContext;
        this.members = members;
    }


    @Override
    public RVAdapter2.RvHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), mLayoutId, parent, false);
        RvHoldView holder = new RvHoldView(binding.getRoot());
        holder.binding = binding;
        return holder;
    }

    @Override
    public void onBindViewHolder(RVAdapter2.RvHoldView holder, int position) {
        holder.binding.setVariable(mVariableId, members.get(position));
        // 别忘记这句代码
        holder.binding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return members == null ? 0 : members.size();
    }

    static class RvHoldView extends RecyclerView.ViewHolder {
        ViewDataBinding binding;

        RvHoldView(View view) {
            super(view);
        }
    }
}
